/*
 * PasteideView.java
 */

package pasteide;

import com.cyp.pasteide.IdeOneService;
import com.cyp.pasteide.pastbinService;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Scanner;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.fife.ui.rtextarea.RTextScrollPane;

/**
 * The application's main frame.
 */
public class PasteideView extends FrameView {

    public PasteideView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = PasteideApp.getApplication().getMainFrame();
            aboutBox = new PasteideAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        PasteideApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        main = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ideoneOutput = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        runCode = new javax.swing.JButton();
        pastebinURI = new javax.swing.JTextField();
        getSource = new javax.swing.JButton();
        newPaste = new javax.swing.JButton();
        selectLang = new javax.swing.JComboBox();
        newPasteURI = new javax.swing.JTextField();
        codeEditor = new javax.swing.JScrollPane();
        pasteBin = new javax.swing.JEditorPane();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        ideInputParams = new javax.swing.JTextArea();
        ideLink = new javax.swing.JTextField();
        jScrollPane4 = new javax.swing.JScrollPane();
        ideErr = new javax.swing.JTextArea();
        ideStatus = new javax.swing.JLabel();
        fileChoose = new javax.swing.JButton();
        fileName = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        settingsMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jLabel3 = new javax.swing.JLabel();

        mainPanel.setName("mainPanel"); // NOI18N

        main.setName("main"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pasteide.PasteideApp.class).getContext().getResourceMap(PasteideView.class);
        ideoneOutput.setBackground(resourceMap.getColor("ideoneOutput.background")); // NOI18N
        ideoneOutput.setColumns(20);
        ideoneOutput.setRows(5);
        ideoneOutput.setText(resourceMap.getString("ideoneOutput.text")); // NOI18N
        ideoneOutput.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("ideoneOutput.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("ideoneOutput.border.titleFont"), resourceMap.getColor("ideoneOutput.border.titleColor"))); // NOI18N
        ideoneOutput.setName("ideoneOutput"); // NOI18N
        jScrollPane2.setViewportView(ideoneOutput);

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setBackground(resourceMap.getColor("jLabel2.background")); // NOI18N
        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        runCode.setText(resourceMap.getString("runCode.text")); // NOI18N
        runCode.setName("runCode"); // NOI18N
        runCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runCodeActionPerformed(evt);
            }
        });

        pastebinURI.setText(resourceMap.getString("pastebinURI.text")); // NOI18N
        pastebinURI.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("pastebinURI.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("pastebinURI.border.titleFont"), resourceMap.getColor("pastebinURI.border.titleColor"))); // NOI18N
        pastebinURI.setName("pastebinURI"); // NOI18N

        getSource.setText(resourceMap.getString("getSource.text")); // NOI18N
        getSource.setName("getSource"); // NOI18N
        getSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getSourceActionPerformed(evt);
            }
        });

        newPaste.setText(resourceMap.getString("newPaste.text")); // NOI18N
        newPaste.setName("newPaste"); // NOI18N
        newPaste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newPasteActionPerformed(evt);
            }
        });

        selectLang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Language" }));
        selectLang.setName("selectLang"); // NOI18N

        newPasteURI.setText(resourceMap.getString("newPasteURI.text")); // NOI18N
        newPasteURI.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("newPasteURI.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("newPasteURI.border.titleFont"), resourceMap.getColor("newPasteURI.border.titleColor"))); // NOI18N
        newPasteURI.setName("newPasteURI"); // NOI18N
        newPasteURI.setVisible(true);

        codeEditor.setName("codeEditor"); // NOI18N

        pasteBin.setContentType("text/plain");
        pasteBin.setName("pasteBin"); // NOI18N
        codeEditor.setViewportView(pasteBin);
        jsyntaxpane.DefaultSyntaxKit.initKit();
        pasteBin.setText("#Load code from "
            + "\n #1) File"
            + "\n #2) pastbin URL"
            + "\n #3) Self");

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        ideInputParams.setColumns(20);
        ideInputParams.setRows(5);
        ideInputParams.setText(resourceMap.getString("ideInputParams.text")); // NOI18N
        ideInputParams.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("ideInputParams.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("ideInputParams.border.titleFont"), resourceMap.getColor("ideInputParams.border.titleColor"))); // NOI18N
        ideInputParams.setName("ideInputParams"); // NOI18N
        jScrollPane3.setViewportView(ideInputParams);

        ideLink.setText(resourceMap.getString("ideLink.text")); // NOI18N
        ideLink.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("ideLink.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("ideLink.border.titleFont"), resourceMap.getColor("ideLink.border.titleColor"))); // NOI18N
        ideLink.setName("ideLink"); // NOI18N

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        ideErr.setColumns(20);
        ideErr.setRows(5);
        ideErr.setText(resourceMap.getString("ideErr.text")); // NOI18N
        ideErr.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("ideErr.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("ideErr.border.titleFont"), resourceMap.getColor("ideErr.border.titleColor"))); // NOI18N
        ideErr.setName("ideErr"); // NOI18N
        jScrollPane4.setViewportView(ideErr);

        ideStatus.setText(resourceMap.getString("ideStatus.text")); // NOI18N
        ideStatus.setName("ideStatus"); // NOI18N

        fileChoose.setText(resourceMap.getString("fileChoose.text")); // NOI18N
        fileChoose.setName("fileChoose"); // NOI18N
        fileChoose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileChooseActionPerformed(evt);
            }
        });

        fileName.setText(resourceMap.getString("fileName.text")); // NOI18N
        fileName.setName("fileName"); // NOI18N

        javax.swing.GroupLayout mainLayout = new javax.swing.GroupLayout(main);
        main.setLayout(mainLayout);
        mainLayout.setHorizontalGroup(
            mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainLayout.createSequentialGroup()
                        .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pastebinURI, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(mainLayout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(fileName, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fileChoose)
                            .addComponent(getSource, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainLayout.createSequentialGroup()
                                .addGap(367, 367, 367)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(selectLang, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(runCode, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(mainLayout.createSequentialGroup()
                                .addComponent(newPasteURI, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(newPaste)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE))
                    .addGroup(mainLayout.createSequentialGroup()
                        .addComponent(codeEditor, javax.swing.GroupLayout.DEFAULT_SIZE, 727, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                            .addComponent(ideLink, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                            .addGroup(mainLayout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(ideStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE))))
                .addGap(35, 35, 35))
        );
        mainLayout.setVerticalGroup(
            mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(mainLayout.createSequentialGroup()
                        .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(fileName)
                                .addComponent(fileChoose))
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(pastebinURI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(newPasteURI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(newPaste)
                            .addComponent(getSource, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(selectLang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(runCode, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainLayout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ideStatus)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ideLink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
                    .addComponent(codeEditor, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(main, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(main, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pasteide.PasteideApp.class).getContext().getActionMap(PasteideView.class, this);
        settingsMenuItem.setAction(actionMap.get("Settings")); // NOI18N
        settingsMenuItem.setText(resourceMap.getString("settingsMenuItem.text")); // NOI18N
        settingsMenuItem.setName("settingsMenuItem"); // NOI18N
        fileMenu.add(settingsMenuItem);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 1182, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(statusMessageLabel)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 770, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(statusMessageLabel)
                            .addComponent(statusAnimationLabel)
                            .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(3, 3, 3))
                    .addComponent(jLabel3)))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void getSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getSourceActionPerformed
           
       String URL= pastebinURI.getText();
        URL=URL.substring(URL.lastIndexOf('/')+1);
        try {
            pasteBin.setText(new pastbinService().getRawPaste(URL));
        } catch (MalformedURLException ex) {
            Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_getSourceActionPerformed

    private void newPasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPasteActionPerformed
        
        try {
            if(getPbKey()==null)
            {
                try {
                    Settings();
                } catch (URISyntaxException ex) {
                    Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            newPasteURI.setText(new pastbinService().createNewPaste(getPbKey(),"paste",pasteBin.getText()));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_newPasteActionPerformed
    
    private void runCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runCodeActionPerformed
      //  ideoneOutput.setText((selectLang.getSelectedIndex()+1)+(String)selectLang.getSelectedItem());
        
        try {
             if(getUsername()==null||getPassword()==null)
            {
                try {
                    Settings();
                } catch (URISyntaxException ex) {
                    Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.out.println("Creating a submission for language"+(String)selectLang.getSelectedItem());
            HashMap res = ideservice.createSubmission(pasteBin.getText(), selectLang.getSelectedIndex()+1, null, true, false);
            String link = (String) res.get("link");         
            ideLink.setText("http://ideone.com/"+link);
            do {
        	res = ideservice.getSubmissionStatus(link);
                if(!"0".equals(res.get("status")+""))
                {
                    Thread.sleep(2000);
                }
        }while(!"0".equals(res.get("status")+""));
            res = ideservice.getSubmissionDetails(link, false, false, true, true, true);

           
                ideErr.setText("# Compiler errors \n"+res.get("cmpinfo").toString()+"\n #Stderr \n"+res.get("stderr").toString());
		String time=res.get("time").toString();
                String date=res.get("date").toString();
                String status=res.get("status").toString();
                String memory=res.get("memory").toString();
                String signal=res.get("signal").toString();
                ideStatus.setText("result: "+resultMap.get(res.get("status"))+"	     time: "+time+"s    memory: "+memory+"kB");
                ideoneOutput.setText(res.get("output").toString());
        } catch (InterruptedException ex) {
            Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
                }
    }//GEN-LAST:event_runCodeActionPerformed

    private void fileChooseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileChooseActionPerformed
        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String fname=file.getName();
            fileName.setText(fname);
            String ext=fname.substring(fname.lastIndexOf(".")+1);
            System.out.println("Extension is"+ext);
            if(langMap.containsKey(ext))
            {
                setContent_type(langMap.get(ext));
                pasteBin.setContentType(langMap.get(ext));
            }
            Scanner sc;
            try {
                sc = new Scanner(new FileInputStream(file));
                StringBuilder sb=new StringBuilder();
                while(sc.hasNextLine())
                {
                    sb.append(sc.nextLine()).append("\n");
                }
                pasteBin.setText(sb.toString());
      //          codeEditor.setViewportView(pasteBin);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PasteideView.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        }
    }//GEN-LAST:event_fileChooseActionPerformed
    
    
    @Action
    public void Settings() throws IOException, URISyntaxException {
       if (settingsBox == null) {
            JFrame mainFrame = PasteideApp.getApplication().getMainFrame();
            settingsBox = new PasteideSettings(mainFrame,true);
            settingsBox.setLocationRelativeTo(mainFrame);
        }
        PasteideApp.getApplication().show(settingsBox);
        initializeProps();
    }

    public  void propChecker() throws IOException
    {
        
    }
    public  void initializeProps() throws IOException, URISyntaxException
    {
        /*Properties prop = new Properties();
        File propfile=new File(getClass().getResource("credentials.properties").toURI());
                prop.load(new FileInputStream(propfile));
            System.out.println("Set username, password, key "+prop.getProperty("ideone.api.user")+","+prop.getProperty("ideone.api.pass")+","+prop.getProperty("pastebin.api.pass"));
 
                setUsername(prop.getProperty("ideone.api.user"));
                setPassword(prop.getProperty("ideone.api.pass"));
                setPbKey(prop.getProperty("pastebin.api.pass"));
          */
        setUsername(pasteide.PasteideSettings.ideone_api_user);
        setPassword(pasteide.PasteideSettings.ideone_api_pass);
        setPbKey(pasteide.PasteideSettings.pastebin_api_pass);
        ideservice = new IdeOneService(getUsername(),getPassword());
                langsmap = ideservice.getLanguages();
                langlist = (String[]) langsmap.values().toArray(new String[0]);
                selectLang.setModel(new DefaultComboBoxModel(langlist));
    }
    
   
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPbKey() {
        return pbKey;
    }

    public void setPbKey(String pbKey) {
        this.pbKey = pbKey;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getContent_type() {
        return content_type;
    }

    public void setContent_type(String content_type) {
        this.content_type = content_type;
    }

    public String[] getLanglist() {
        return langlist;
    }

    public void setLanglist(String[] langlist) {
        this.langlist = langlist;
    }

   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane codeEditor;
    private javax.swing.JButton fileChoose;
    private javax.swing.JLabel fileName;
    private javax.swing.JButton getSource;
    private javax.swing.JTextArea ideErr;
    private javax.swing.JTextArea ideInputParams;
    private javax.swing.JTextField ideLink;
    private javax.swing.JLabel ideStatus;
    private javax.swing.JTextArea ideoneOutput;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JPanel main;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton newPaste;
    private javax.swing.JTextField newPasteURI;
    private javax.swing.JEditorPane pasteBin;
    private javax.swing.JTextField pastebinURI;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JButton runCode;
    private javax.swing.JComboBox selectLang;
    private javax.swing.JMenuItem settingsMenuItem;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    
    private JDialog aboutBox;
    private JDialog settingsBox;
    
    private  String username;
    private  String password;
    private  String pbKey;
    private String content_type="text/plain";
    private IdeOneService ideservice;
    private HashMap langsmap;
    private String[] langlist;
    private static final Map<Integer, String> resultMap;
    private static final Map<String, String> langMap;
    static {
        Map<Integer, String> aMap = new HashMap<Integer, String>();
        aMap.put(0, "NOT RUNNING");
        aMap.put(11, "COMPILATION ERROR");
        aMap.put(12, "RUNTIME ERROR");
        aMap.put(13, "COMPILATION ERROR");
        aMap.put(15, "SUCCESS");
        aMap.put(17, "MEMORY LIMIT EXCEEDED");
        aMap.put(19, "ILLEGAL SYSTEM CALL");
        aMap.put(20, "INTERNAL ERROR");
        resultMap = Collections.unmodifiableMap(aMap);
        
        Map<String, String> bMap = new HashMap<String, String>();
        bMap.put("c", "text/c");
        bMap.put("cpp", "text/cpp");
        bMap.put("java", "text/java");
        bMap.put("js", "text/javascript");
        bMap.put("txt", "text/plain");
        bMap.put("hs", "text/haskell");
        bMap.put("properties", "text/properties");
        bMap.put("groovy", "text/groovy");
        bMap.put("xml", "text/xml");
        bMap.put("py", "text/python");
        bMap.put("sql", "text/sql");
        bMap.put("rb", "text/ruby");
        bMap.put("sh", "text/bash");
        
        langMap = Collections.unmodifiableMap(bMap);
    }
        
    
}
